package com.chris.cc.user.controller;

import com.chris.cc.common.exception.InvalidArgumentException;
import com.chris.cc.common.web.R;
import com.chris.cc.user.model.dto.RoleDto;
import com.chris.cc.user.model.vo.UserInfoVO;
import com.chris.cc.user.service.wrapper.PermissionServiceWrapper;
import com.chris.cc.user.service.wrapper.UserServiceWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: haoka (haokang207@126.com)
 * Date: 2018/8/15 14:09
 * 角色
 **/
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private PermissionServiceWrapper permissionServiceWrapper;
    @Autowired
    private UserServiceWrapper userServiceWrapper;

    @GetMapping
    public R query(Authentication authentication, Boolean enable, int page, int limit, boolean isAsc, String keyword) {
        String tel = authentication.getPrincipal().toString();
        UserInfoVO user = userServiceWrapper.getByTel(tel);
        PageRequest pageRequest = PageRequest.of(page - 1, limit, Sort.by(isAsc ? Sort.Direction.ASC : Sort.Direction.DESC,
                "name"));
        return R.builder().data(permissionServiceWrapper.queryRole(user.getUserId(), enable, keyword, pageRequest)).build();
    }

    @PostMapping
    public R add(@RequestBody RoleDto role) {
        try {
            permissionServiceWrapper.addRole(role);
            return R.builder().build();
        } catch (InvalidArgumentException e) {
            log.error("add role failed.", e);
            return R.builder().ret(-1).build();
        }
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.builder().data(permissionServiceWrapper.getRoleById(id)).build();
    }

    @GetMapping("/get/{userId}")
    public R getByUserId(@PathVariable("userId") Long userId) {
        return R.builder().data(permissionServiceWrapper.getRoleByUserId(userId)).build();
    }

    @GetMapping("/{id}/{enable}")
    public R toggle(@PathVariable("id") Long id, @PathVariable("enable") boolean enable) {
        permissionServiceWrapper.toggleRole(id, enable);
        return R.builder().build();
    }

    @PutMapping
    public R update(@RequestBody RoleDto role) {
        try {
            permissionServiceWrapper.updateRole(role);
            return R.builder().build();
        } catch (InvalidArgumentException e) {
            log.error("update role failed.", e);
            return R.builder().ret(-1).build();
        }
    }

    @GetMapping("/permit/{roleId}")
    public R getPermitRoleByRoleId(@PathVariable("roleId") Long roleId) {
        return R.builder().data(permissionServiceWrapper.getPermitRoleByRoleId(roleId)).build();
    }

    @GetMapping("/permit")
    public R getPermitRoleByUserId(Authentication authentication) {
        Long userId = userServiceWrapper.getByTel(authentication.getPrincipal().toString()).getUserId();
        return R.builder().data(permissionServiceWrapper.getPermittedRoles(userId)).build();
    }

    @PutMapping("/permit/{roleId}")
    public R updatePermitRole(@PathVariable("roleId") Long roleId, @RequestBody Long[] permitRoleIds) {
        permissionServiceWrapper.updatePermitRole(roleId, permitRoleIds);
        return R.builder().build();
    }
}
